/* 
 * 
 * Copyright 2011 The University of Adelaide
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *  
 */

package au.org.aekos.ands.alis.web;

import static au.org.aekos.ands.global.Util.serialise;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import au.org.aekos.ands.alis.data.Visit;
import au.org.aekos.ands.alis.process.GenerateActivity;
import au.org.aekos.ands.alis.process.GenerateCollection;
import au.org.aekos.ands.global.Schema;
import au.org.aekos.ands.global.Schema.EntityType;

/**
 * The controller for REST-oriented provision of activity and collection information
 * from the
 * <a href="http://e-nrims.dwlbc.sa.gov.au/alis/">Arid Lands Information System (ALIS)</a>
 * of the
 * <a href="http://www.sa.gov.au">South Australian Government</a>
 * <a href="http://www.environment.sa.gov.au">Department of Environment and Natural Resources</a>.
 */
@RequestMapping("/")
@Controller
public class ALIS {
	public static final String ACTIVITY = "ALIS";
	
	private Schema schema;
	private Map<String,List<Visit>> visitTypes;

	/**
	 * Instantiates a new controller for loading ALIS information. Auxiliary and overlay
	 * metadata is provided by the metadata properties located on the classpath.
	 *
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public ALIS() throws IOException {
		schema = new Schema(getClass().getClassLoader().getResourceAsStream("metadata.properties"));				
		loadVisitTypes();
	}
	
    /**
     * Populate the visit types.
     */
    private void loadVisitTypes() {
		List<Visit> visits = Visit.findAllVisits();

		visitTypes = new HashMap<String,List<Visit>>();
		
		for (Visit visit : visits) {
			String visitType = visit.getVisitType();
			if (schema.isIgnored(EntityType.COLLECTION, visitType)) { continue; }
			
			List<Visit> visitList = visitTypes.get(visitType);
			if (visitList == null) {
				visitList = new ArrayList<Visit>();
				visitTypes.put(visitType, visitList);
			}
			
			visitList.add(visit);
		}
    }

	// Activity

    /**
	 * Returns the activity identifiers. In the case of ALIS, there is only one activity: ALIS itself.
	 *
	 * @return the identifiers.
	 */
	@RequestMapping(value = "/activities", method = RequestMethod.GET)
    public @ResponseBody Collection<String> listActivities() {
    	return Arrays.asList(new String[] { ACTIVITY });
    }
    
    /**
     * Returns the specified activity.
     *
     * @param id the activity identifier.
     * @return the activity (as a RIF-CS XML string), if one corresponds to that identifier, otherwise null.
     */
    @RequestMapping(value = "/activity/{id:.*}", method = RequestMethod.GET)
    public @ResponseBody String getActivity(@PathVariable String id) {
    	return serialise(new GenerateActivity(schema).generateActivity(id));
    }
	
	// Collection
	
    /**
	 * Returns the collection identifiers.
	 *
	 * @return the identifiers.
	 */
	@RequestMapping(value = "/collections", method = RequestMethod.GET)
    public @ResponseBody Collection<String> listCollections() {
    	return visitTypes.keySet();
    }

    /**
     * Returns the specified collection.
     *
     * @param id the collection identifier.
     * @return the collection (as a RIF-CS XML string), if one corresponds to that identifier, otherwise null.
     */
    @RequestMapping(value = "/collection/{id:.*}", method = RequestMethod.GET)
    public @ResponseBody String getCollection(@PathVariable String id) {
    	return serialise(new GenerateCollection(schema).generateCollection(id, visitTypes.get(id)));
    }
}
